package com.woniuxy.rbac.web;

import com.woniuxy.rbac.entity.User;
import com.woniuxy.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 用户操作Controller类
 *
 * @Auther: 老谭（<a href="http://www.woniuxy.com">蜗牛学苑</a>）
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/add")
    public String add() {
        return "user/user_add";
    }

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public String add(User user, Model model) {
        userService.add(user);
        return "redirect:/user/getall"; //前台重定向
    }

    @GetMapping("getall")
    public String getAll(Model model) {
        model.addAttribute("users", userService.getAll());
        return "user/user_list";
    }

    @GetMapping("/del/{id}")
    public String del(@PathVariable int id){
        userService.delById(id);
        return "redirect:/user/getall"; //前台重定向
    }



}
